package com.ptu.servlet;

import com.ptu.entity.PageTools;
import com.ptu.entity.Payment;
import com.ptu.service.PaymentService;
import com.ptu.service.impl.PaymentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Date;
import java.util.List;

/**
 *
 */
@WebServlet("/ptu/PaymentServlet")
public class PaymentServlet extends BaseServlet {
    private PaymentService paymentService = new PaymentServiceImpl();

    private Payment createpay(HttpServletRequest req, HttpServletResponse resp){
        Integer pid = toInteger(req.getParameter("pid"));
        String payempid = req.getParameter("payempid");
        Double amount = toDouble(req.getParameter("amount"));
        Date paytime = toDate(req.getParameter("paytime"));
        String paytype = req.getParameter("paytype");
        String paydesc = req.getParameter("paydesc");

        Payment pay = new Payment(pid,payempid,amount,paytime,paytype,paydesc);
        return pay;
    }

    // 添加支出
    protected void paymentSave(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 接受页面的数据
        Payment pay = createpay(req, resp);

        // 2. 数据的处理
        int save = paymentService.addpayment(pay);

        // 3. 给用户做出响应
        if (save > 0) {
            resp.sendRedirect(req.getContextPath() + "/ptu/PaymentServlet?method=showPayPage");
        } else {
            req.setAttribute("msg", "添加失败");
            req.getRequestDispatcher("/addPayment.jsp").forward(req, resp);
        }
    }

    protected void payChange(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 接受页面中的数据
        Payment pid = createpay(req, resp);

        // 2. 数据的处理
        int change = paymentService.changepay(pid);

        // 3. 给用户做出响应
        if (change > 0) {
            resp.sendRedirect(req.getContextPath() + "/ptu/PaymentServlet?method=showPayPage");
        } else {
            req.setAttribute("msg", "修改失败");
            req.getRequestDispatcher("/payUpdate.jsp").forward(req, resp);
        }
    }

    // 查询所有支出信息
    private void payFindAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 查询所有支出信息
        List<Payment> payList = paymentService.findAll();

        req.setAttribute("payList", payList);

        req.getRequestDispatcher("/expenseList.jsp").forward(req, resp);
    }

    // 查询支出
    protected void paymentFindMore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 接受页面的数据
        String paytime = req.getParameter("paytime");

        String payempid = req.getParameter("payempid");

        String paytype = req.getParameter("paytype");

        // 2. 数据的处理

        // 查询符和条件集合
        List<Payment> payMore = paymentService.findPayMore(paytime, payempid, paytype);
        PageTools<Payment> payAll =new PageTools<>();
        // 3. 给用户做出响应
        payAll.setList(payMore);

        req.setAttribute("payAll", payAll);
//        req.setAttribute("payMore",payMore);
        req.getRequestDispatcher("/expenseList.jsp").forward(req, resp);
    }

    // 查询指定支出
    protected void payFindOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 获取前端数据
        Integer pid = toInteger(req.getParameter("pid"));
//        String payempid = req.getParameter("payempid");

        // 2. 数据处理
        Payment pay = paymentService.findPayByPid(pid);

        // 3. 返回结果

        if (pay != null) {
            req.setAttribute("pay", pay);
            req.setAttribute("pid", pid);
            req.getRequestDispatcher("/payUpdate.jsp").forward(req, resp);
        }
    }

    // 删除支出
    protected void paymentRemove(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer pid = toInteger(req.getParameter("pid"));
        int i = paymentService.removepay(pid);
        req.setAttribute("i", i);
        showPayPage(req, resp);
    }

//    分页查询
    public void showPayPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 第一次访问的验证，如果没有传递参数，设置默认值
        String pageSizeReq = req.getParameter("pageSize");

        int pageSize = 2;   // 查询大小
        if(pageSizeReq != null && !pageSizeReq.equals("")){
            pageSize = Integer.parseInt(pageSizeReq);
        }

        String pageNumberStr = req.getParameter("pageNumber");
        int pageNumber = 1; // 开始值
        if(pageNumberStr!=null&&!pageNumberStr.equals("")){
            pageNumber = Integer.parseInt(pageNumberStr);
        }
        PageTools<Payment> payAll = paymentService.findPayAll(pageSize, pageNumber);
        req.setAttribute("payAll",payAll);
        req.getRequestDispatcher("/expenseList.jsp").forward(req, resp);

    }

//    统计
    public void total(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int totalXMKFCount = paymentService.selectXMKFCountService();
        int totalStuCount = paymentService.selectStuCountService();
        int totalRYWBCount = paymentService.selectRYWBCountService();
        int totalBMCount = paymentService.selectBMCountService();
        int totalXMKFPay = paymentService.selectXMKFPayService();
        int totalStuPay = paymentService.selectStuPayService();
        int totalRYWBPay = paymentService.selectRYWBPayService();
        int totalBMPay = paymentService.selectBMPayService();
        req.setAttribute("totalXMKFCount",totalXMKFCount);
        req.setAttribute("totalStuCount",totalStuCount);
        req.setAttribute("totalRYWBCount",totalRYWBCount);
        req.setAttribute("totalBMCount",totalBMCount);
        req.setAttribute("totalXMKFPay",totalXMKFPay);
        req.setAttribute("totalStuPay",totalStuPay);
        req.setAttribute("totalRYWBPay",totalRYWBPay);
        req.setAttribute("totalBMPay",totalBMPay);
        req.getRequestDispatcher("/expensestatis.jsp").forward(req, resp);
    }

    private Integer toInteger(String value) {
        return value == null ? null : Integer.parseInt(value);
    }

    private Double toDouble(String value) {
        return value == null ? null : Double.parseDouble(value);
    }

    private Date toDate(String value){
        try {
            return Date.valueOf(value);
        }catch (IllegalArgumentException i) {
            return null;
        }
    }

}
